package cn.xorange.commons.data.entity;

import cn.xorange.commons.data.annotation.MColumn;
import cn.xorange.commons.data.constants.QueryType;
import cn.xorange.commons.data.utils.DataUtil;
import cn.xorange.commons.data.utils.ParamUtil;
import cn.xorange.commons.exception.DataException;
import cn.xorange.commons.utils.lang.ObjectUtils;
import cn.xorange.commons.utils.lang.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Objects;
import java.util.Set;

/**
 * @author yangjian
 */
@Slf4j
public class Param {

    /**
     * 原 Param
     * @param field 实体类列
     * @param colName sqlCol
     * @param value 值
     * @param typeName 批处理新增、修改空字段也需要进行处理，空字符获取不到值的类型，报错，需要告诉myBatis 字段的类型
     */
    public Param(Field field,String colName,Object value,String typeName){
         if(Objects.isNull(field)){
             this.fieldName = colName;
         }else {
             this.fieldName = field.getName();
         }
         this.column = colName;
         this.value = value;
         if(Objects.nonNull(typeName)){
             /// 批处理告诉字段类型 以防止报错
             this.jdbcType = this.setJdbcType(typeName);
         }

    }

    public Param(String fieldName,String colName,String queryType,Object value){
        this.fieldName = fieldName;
        this.column = colName;
        this.queryType = queryType;
        this.value = value;
        if(Objects.nonNull(value)){
            if(ObjectUtils.isCollection(value) || ObjectUtils .isArray(value)){
                if (queryType.equals(QueryType.EQUAL)){
                    this.queryType =  QueryType.IN;
                } else if(queryType.equals(QueryType.NOTEQUAL)){
                    this.queryType = QueryType.NOTIN;
                }
            }
        }

    }
    private String column;
    private String fieldName;
    private String queryType;
    private Object value;
    private String jdbcType;

    public String getColumn() {
        return column;
    }
    public String getFieldName() { return fieldName;}
    public String getQueryType() {
        return queryType;
    }
    public Object getValue() {
        return value;
    }
    public String getJdbcType() { return jdbcType; }
    private String setJdbcType(String typeName){
        return DataUtil.getJdbcType(typeName);
    }


}
